package com.linux.huhx.utils;

import org.junit.Test;
import org.springframework.util.Assert;

import java.text.MessageFormat;
import java.util.*;

/**
 * Created by huhx on 2017-06-14.
 */
public class CodeRegrexUtils {
    /**
     * ----------------------------------- base ----------------------------------
     */
    // ${name} from map
    public static String baseReplaceFromMap(String string, Map<String, String> map) {
        Set<String> set = map.keySet();
        for (String str : set) {
            string = string.replace("${" + str + "}", map.get(str)); // 这里不能用replaceAll
        }
        return string;
    }

    // {name} from map
    public static String baseReplaceFromMap2(String string, Map<String, String> map) {
        Set<String> set = map.keySet();
        for (String str : set) {
            string = string.replace("{" + str + "}", map.get(str)); // 这里不能用replaceAll
        }
        return string;
    }

    // {name} from javaBean
    public static <T> String baseReplaceFromJavaBean(String string, T object) {
        Map<String, String> stringObjectMap = BeanUtils.bean2MapString(object);
        return baseReplaceFromMap2(string, stringObjectMap);
    }

    // {0} from arrays
    public String baseReplaceFromArray(String string, List<String> list) {
        String[] arrays = ToolUtils.transListInfoArrays(list);
        return MessageFormat.format(string, arrays);
    }

    /**
     * ----------------------------------- list ----------------------------------
     */
    public List<String> simpleReplaceFromMap(List<String> lists, Map<String, String> map) {
        List<String> resultLists = new ArrayList<>();
        for (String string : lists) {
            resultLists.add(baseReplaceFromMap(string, map));
        }
        return resultLists;
    }

    public List<String> simpleReplaceFromMap2(List<String> lists, Map<String, String> map) {
        List<String> resultLists = new ArrayList<>();
        for (String string : lists) {
            resultLists.add(baseReplaceFromMap2(string, map));
        }
        return resultLists;
    }

    // {name} from javaBean
    public static <T> List<String> baseReplaceFromJavaBean(String string, List<T> objects) {
        List<String> resultLists = new ArrayList<>();
        for (T object : objects) {
            resultLists.add(baseReplaceFromJavaBean(string, object));
        }
        return resultLists;
    }

    public List<String> simpleReplaceFromArray(List<String> lists, List<String> replaceList) {
        List<String> resultLists = new ArrayList<>();
        for (String string : lists) {
            resultLists.add(baseReplaceFromArray(string, replaceList));
        }
        return resultLists;
    }


    /**
     * ----------------------------------- test ----------------------------------
     */

    @Test
    public void baseReplaceFromArray_test() {
        List<String> list = new ArrayList<>();
        list.add("huhx");
        list.add("L。");
//        Assert.assertEquals("huhx love liuling", baseReplaceFromArray("{0} love {1}", list));
        System.out.println(baseReplaceFromArray("{0} love {1}", list)); // huhx love L。
    }

    @Test
    public void transListInfoArrays_test() {
        List<String> list = new ArrayList<>();
        list.add("huhx");
        list.add("L。");
        System.out.println(Arrays.toString(ToolUtils.transListInfoArrays(list)));
    }

    @Test
    public void beanUtil_test() {
        Assert.notNull(null, "Bean object must not be null");
    }
}
